<template>
  <a-modal
    :open="open"
    width="700px"
    :title="`${isEdit ? '编辑协定方' : '新增协定方'}`"
    centered
    :ok-button-props="{ disabled: !isCompleted }"
    ok-text="保存"
    @ok="onOk"
    @cancel="onCancel"
  >
    <a-form :model="formState" class="form-style-flex" :label-col="{ style: { width: '100px' } }">
      <a-form-item label="协定方类别" class="w-full">
        <a-radio-group v-model:value="formState.sylb">
          <a-radio :value="0">个人</a-radio>
          <a-radio :value="1">科室</a-radio>
          <a-radio :value="2">全院</a-radio>
          <a-radio :value="3">经方</a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item label="协定方名称" v-bind="validateInfos.mc" class="w-1/2">
        <a-input v-model:value="formState.mc" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="分类" class="w-1/2">
        <a-input v-model:value="formState.setType" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="来源" class="w-1/2">
        <a-input v-model:value="formState.setSource" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="功用" class="w-1/2">
        <a-input v-model:value="formState.setEffect" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="主治" class="w-full">
        <a-input v-model:value="formState.indications" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="加减" class="w-full">
        <a-input v-model:value="formState.addSub" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="协定方说明" class="w-full">
        <a-textarea v-model:value="formState.memo" :rows="5" placeholder="请输入" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup>
import { ref } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'

const emit = defineEmits(['update:open', 'success'])
const props = defineProps({
  open: {
    type: Boolean,
    default: false
  },
  orderData: {
    type: Object,
    default: () => ({})
  }
})
const formState = ref({
  sylb: 0,
  mc: '',
  setType: '',
  setSource: ''
})
const rulesState = ref({
  mc: [{ required: true, message: '请输入协定方名称' }]
})
const { isCompleted, resetFields, validate, validateInfos } = useForm(formState, rulesState)
const isEdit = ref(false)

const onCancel = () => {
  emit('update:open', false)
  resetFields()
}
const onOk = async () => {
  await validate()
  const { code, msg } = await post('/outp/template/doAddClinicalOrderPackageAndItem', {
    orderPackageItem: props.orderData,
    orderPackageRequest: {
      ...formState.value,
      tclb: 0,
      czcf: 2,
      tcPrice: formState.value.tcPrice || 0,
      enablePackagePricing: formState.value.enablePackagePricing ? 1 : 0,
      isRegTc: formState.value.isRegTc ? 1 : 0
    }
  })
  if (code === 0) {
    message.success('保存成功')
    emit('success')
    onCancel()
  } else {
    message.error(msg)
  }
}
</script>
